package com.seari.scheduling.plan;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.seari.Utils.DateUtils;
import com.seari.bean.PersonInfoBean;
import com.seari.bean.ReqPlanShiftBean;

@Component
public class PlanFourClass implements PlanScheduClass
{
	
	@Override
	public List<PersonInfoBean> planScheduling(ReqPlanShiftBean bean,List<PersonInfoBean> pesonInfo)
	{
		Map<Integer,PersonInfoBean> per = new HashMap<>();
		List<Integer> onDuty = new ArrayList<>();
		List<PersonInfoBean> persons = new ArrayList<>();
		for (PersonInfoBean person : pesonInfo)
		{
			Integer id = person.getUid();
			onDuty.add(id);
			per.put(id, person);
		}
		
		int days = 0;
		if(bean.getPlanShiftType().equals("week")) {
			//
		}
		days = DateUtils.getDaysOfMonth(bean.getReqShiftDate()); //获取当月天数   
		List<Integer> morningShift = new ArrayList<>(days);
		List<Integer> nightShift = new ArrayList<>(days);

		int more = 2;  //早班补足量
		int nire = 3;  //晚班补足量

		morningShift = morningShiftScheduling(more,days,onDuty);
		nightShift = nightShiftScheduling(nire,days,onDuty);

		int[] temp = new int[2];
		for (int i = 0; i < morningShift.size(); i++)
		{
			Integer pidMon = morningShift.get(i);
			PersonInfoBean personInfoMon = per.get(pidMon);
			personInfoMon.getShiftInfo().add(i, "早");
			temp[0] = pidMon;
			
			Integer pidNig = nightShift.get(i);
			PersonInfoBean personInfoNig = per.get(pidNig);
			personInfoNig.getShiftInfo().add(i, "晚");
			temp[1] = pidNig;
			Arrays.sort(temp);
			
			for (int pid : onDuty)
			{
				if(Arrays.binarySearch(temp, pid) < 0) {
					PersonInfoBean personInfo = per.get(pid);
					personInfo.getShiftInfo().add(i, "休");
				}
			}
			
		}
		Iterator<Entry<Integer, PersonInfoBean>> entries = per.entrySet().iterator(); 
		while (entries.hasNext()) { 
		  Entry<Integer, PersonInfoBean> entry = entries.next(); 
		  persons.add(entry.getValue());
		}
		
		return persons;
	}
	

	public List<Integer> morningShiftScheduling(int more,int days,List<Integer> onDuty)
	{



		List<Integer> morningShift = new ArrayList<>(days);
		int mormod = (days - more) % 4;  //补足的数量
		int moslice = (days - more) / 4;
		
		for (int i = more; i >= 1; i--)
		{
			morningShift.add(onDuty.get(onDuty.size()-i));
		}
		
		for (int i = 0; i < moslice; i++)
		{
			morningShift.addAll(onDuty);
		}
		
		for (int i = 0; i < mormod; i++)
		{
			morningShift.add(onDuty.get(i));
		}
		
		more = 4 - mormod;
		
		return morningShift;
	}

	public List<Integer> nightShiftScheduling(int nire,int days,List<Integer> onDuty)
	{

		List<Integer> nightShift = new ArrayList<>(days);
		int nigmod = (days - nire) % 4;
		int nislice = (days - nire) / 4;
		
		for(int i = nire; i >= 1;i--) {
			nightShift.add(onDuty.get(onDuty.size()-i));
		}
		
		for (int i = 0; i < nislice; i++)
		{
			nightShift.addAll(onDuty);
		}
		
		for (int i = 0; i < nigmod; i++)
		{
			nightShift.add(onDuty.get(i));
		}
		
		nire = 4 - nigmod;
		
		return nightShift;
	}
	
}
